package com.flight.trans.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.base.BaseRecyclerAdapter;
import com.flight.trans.base.BaseViewHolder;
import com.flight.trans.bean.StoreroomInventoryDetailBean.StoreroomInventoryDetailData;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.LogUtils;
import com.flight.trans.util.ToastUtils;

import java.util.List;

/**
 * 盘库
 */
public class StoreroomInventoryDetailsAdapter extends BaseRecyclerAdapter<StoreroomInventoryDetailData> {

    private boolean isEditor ;
    private int turnType ;
    private boolean isConfirm ;

    public StoreroomInventoryDetailsAdapter(Context mContext, List<StoreroomInventoryDetailData> mList, boolean isEditor , int turnType , boolean isConfirm) {
        super(mContext, mList);
        this.isEditor = isEditor;
        this.turnType = turnType ;
        this.isConfirm = isConfirm ;
    }

    @Override
    public int setContentView() {
        return R.layout.adapter_storeroom_inventory_details;
    }

    @Override
    protected void bindData(BaseViewHolder holder, StoreroomInventoryDetailData data, int position) {
        LinearLayout mLlContainerItem = holder.findViewById(R.id.mLlContainerItem);

        TextView mTvPosition = holder.findViewById(R.id.mTvPosition);
        TextView mTvOrderNo = holder.findViewById(R.id.mTvOrderNo);
        TextView mTvProductName = holder.findViewById(R.id.mTvProductName);
        TextView mTvStorageArea = holder.findViewById(R.id.mTvStorageArea);
        TextView mTvRealCount = holder.findViewById(R.id.mTvRealCount);
        TextView mTvInventoryCount = holder.findViewById(R.id.mTvInventoryCount);
        TextView mTvInventoryStatus = holder.findViewById(R.id.mTvInventoryStatus);
        TextView mTvDeep = holder.findViewById(R.id.mTvDeep);
        TextView mTvRemarks = holder.findViewById(R.id.mTvRemarks);
        TextView mTvInventoryTime = holder.findViewById(R.id.mTvInventoryTime);

        RelativeLayout mRlOperation = holder.findViewById(R.id.mRlOperation);
        Button mBtnOperation = holder.findViewById(R.id.mBtnOperation);

        boolean isSign = data.getCheckSign() == 1 ;

        mRlOperation.setVisibility(isEditor ? View.VISIBLE : View.GONE);

        setTextColor(mTvPosition , isSign);
        setTextColor(mTvOrderNo , isSign);
        setTextColor(mTvProductName , isSign);
        setTextColor(mTvStorageArea , isSign);
        setTextColor(mTvRealCount , isSign);
        setTextColor(mTvInventoryCount , isSign);
        setTextColor(mTvInventoryStatus , isSign);
        setTextColor(mTvDeep , isSign);
        setTextColor(mTvRemarks , isSign);
        setTextColor(mTvInventoryTime , isSign);

        mTvDeep.setText(turnType == 0 ? data.getOrap() : data.getDeap());
        mTvPosition.setText(String.valueOf(position + 1));

        mTvOrderNo.setText(data.getOrderNo());
        mTvProductName.setText(data.getTradeName());
        mTvStorageArea.setText(data.getWarehouseAreaNames());

        mTvRealCount.setText(String.valueOf(data.getNumber() != 0 ? data.getNumber() : ""));

        // 盘库件数
        if(data.getCheckNumber() == 0){
            mTvInventoryCount.setHint(String.valueOf(data.getNumber()));
        }else {
            mTvInventoryCount.setText(String.valueOf(data.getCheckNumber()));
        }

        mTvInventoryStatus.setText(data.getAnomalyCnt() != 0 ? String.valueOf(data.getAnomalyCnt()) : "无");
        mTvRemarks.setText(data.getRemark());
        mTvInventoryTime.setText(CommonUtils.getTimeNorm(data.getCreateTime()));

        if(!isSign) {
            setOnTextClickListener(mTvInventoryCount, position);
            setOnTextClickListener(mTvInventoryStatus, position);
        }else {
            mTvInventoryCount.setClickable(false);
            mTvInventoryStatus.setClickable(false);
        }

        // 空白记录
        if(data.isBlankRecord()){
            mLlContainerItem.setBackgroundColor(mContext.getColor(R.color.color_cyan));

            if(!isSign) {
                setOnTextClickListener(mTvOrderNo, position);
                setOnTextClickListener(mTvProductName, position);
                setOnTextClickListener(mTvStorageArea, position);
                setOnTextClickListener(mTvRealCount, position);
            }

            mTvOrderNo.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            mTvProductName.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            mTvStorageArea.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            mTvRealCount.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));

            setOnItemLongClickListener(mLlContainerItem , position);
        }

        mTvRemarks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(data.getRemark()))
                    ToastUtils.showToast(data.getRemark());
            }
        });

        setOnTextClickListener(mBtnOperation, position);

        if(isConfirm){
            mBtnOperation.setText(mContext.getString(isSign ? R.string.text_cancel : R.string.text_confirm));
            mBtnOperation.setBackground(mContext.getDrawable(isSign ? R.drawable.selector_shape_red : R.drawable.selector_button));
        }else {
            mBtnOperation.setText(mContext.getString(R.string.button_issue));
        }
    }

    private void setTextColor(TextView textView , boolean isSelected){
        textView.setTextColor(mContext.getResources().getColor(isSelected ? R.color.color_blue : R.color.text_color));
    }

    public void setConfirm(boolean isConfirm){
        this.isConfirm = isConfirm;
        updateAdapter(mList);
    }
}
